package com.ywger.integration.resttemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author chenmeng
 * @date 2021-07-17 15:50
 * @desc
 */
@Component
public class RestTemplateHelper {


    @Autowired
    private RestTemplate restTemplate;


    private  String get(String url){

        String result = restTemplate.getForObject(url,String.class);

        return result;

    }

    private  String get(String url,Object ... uriVariables){

        String result = restTemplate.getForObject(url,String.class,uriVariables);

        return result;

    }

    private String get(String url, Map<String, ?> uriVariables){
        String result = restTemplate.getForObject(url,String.class,uriVariables);
        return result;
    }



    private  String post(String url,Object object){

        String result = restTemplate.postForObject(url,object,String.class);

        return result;

    }

    private  String post(String url,Object object,Object ... uriVariables){

        String result = restTemplate.postForObject(url,object,String.class,uriVariables);

        return result;

    }

    private String post(String url,Object object, Map<String, ?> uriVariables){
        String result = restTemplate.postForObject(url,object,String.class,uriVariables);
        return result;
    }


    public <T> T get(String url,Class<T> responseType){
        String result = get(url);
        T  responseObj = JSON.parseObject(result,responseType);
        return responseObj;
    }

    public <T> T get(String url, TypeReference<T> responseTypeReference){
        String result = get(url);
        T  responseObj = JSON.parseObject(result,responseTypeReference);
        return responseObj;
    }

    public <T> T get(String url,Class<T> responseType,Object ... uriVariables){
        String result = get(url,uriVariables);
        T  responseObj = JSON.parseObject(result,responseType);
        return responseObj;
    }

    public <T> T get(String url, TypeReference<T> responseTypeReference, Object ... uriVariables){
        String result = get(url,uriVariables);
        T  responseObj = JSON.parseObject(result,responseTypeReference);
        return responseObj;
    }

    public <T> T get(String url,Class<T> responseType,Map<String,?> uriVariables){
        String result = get(url,uriVariables);
        T  responseObj = JSON.parseObject(result,responseType);
        return responseObj;
    }

    public <T> T get(String url, TypeReference<T> responseTypeReference, Map<String,?> uriVariables){
        String result = get(url,uriVariables);
        T  responseObj = JSON.parseObject(result,responseTypeReference);
        return responseObj;
    }



    public <T> T post(String url,Object object,Class<T> responseType){
        String result = post(url,object);
        T  responseObj = JSON.parseObject(result,responseType);
        return responseObj;
    }

    public <T> T post(String url, Object object, TypeReference<T> responseTypeReference){
        String result = post(url,object);
        T  responseObj = JSON.parseObject(result,responseTypeReference);
        return responseObj;
    }

    public <T> T post(String url,Object object,Class<T> responseType,Object ... uriVariables){
        String result = post(url,object,uriVariables);
        T  responseObj = JSON.parseObject(result,responseType);
        return responseObj;
    }

    public <T> T post(String url, Object object, TypeReference<T> responseTypeReference, Object ... uriVariables){
        String result = post(url,object,uriVariables);
        T  responseObj = JSON.parseObject(result,responseTypeReference);
        return responseObj;
    }

    public <T> T post(String url,Object object,Class<T> responseType,Map<String,?> uriVariables){
        String result = post(url,object,uriVariables);
        T  responseObj = JSON.parseObject(result,responseType);
        return responseObj;
    }

    public <T> T post(String url, Object object, TypeReference<T> responseTypeReference, Map<String,?> uriVariables){
        String result = post(url,object,uriVariables);
        T  responseObj = JSON.parseObject(result,responseTypeReference);
        return responseObj;
    }


    private String uploadFile(String url, MultiValueMap multiValueMap){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(multiValueMap, httpHeaders);
        String result = restTemplate.postForObject(url, entity, String.class);
        return result;
    }


    public <T> T uploadFile(String url, MultiValueMap multiValueMap, Class<T> responseType){
        String result = uploadFile(url,multiValueMap);
        T  responseObj = JSON.parseObject(result,responseType);
        return responseObj;
    }

    public <T> T uploadFile(String url, MultiValueMap multiValueMap, TypeReference<T> responseTypeReference){
        String result = uploadFile(url,multiValueMap);
        T  responseObj = JSON.parseObject(result,responseTypeReference);
        return responseObj;
    }


    /**
     * DEMO
     * OrgCode orgCode = new OrgCode();
     *         orgCode.setOrgCode("110000000401");
     *         Result result = restTemplate.post("http://192.168.9.65:8081/bank/top.json"
     *                 ,orgCode,Result.class);
     *         System.out.println(result);
     *
     *         Result result2 = restTemplate.post("http://192.168.9.65:8081/bank/top.json"
     *                 ,orgCode,new TypeReference<Result<OrgInfo>>(){});
     *
     *         System.out.println(result2);
     *
     *
     *         MultiValueMap multiValueMap = new LinkedMultiValueMap();
     *         List<FileSystemResource> files = new ArrayList<>();
     *         files.add(new FileSystemResource(new File("F:\\zccp\\resttemplateDemo\\src\\main\\java\\com\\resttmplate\\demo\\rest\\inteceptor\\RestRespErrorHandler.java")));
     *         files.add(new FileSystemResource(new File("F:\\zccp\\resttemplateDemo\\src\\main\\java\\com\\resttmplate\\demo\\rest\\inteceptor\\RestLogInterceptor.java")));
     *         multiValueMap.addAll("file",files);
     *         multiValueMap.add("method", "method");
     *         multiValueMap.add("reqId", "reqId");
     *         multiValueMap.add("content", "body.getContent()");
     *         multiValueMap.add("entCode"," body.getEntCode()");
     *         multiValueMap.add("timestamp", "body.getTimestamp()");
     *         multiValueMap.add("signature", "body.getSignature()");
     *
     *
     *         Result result3 = restTemplate.uploadFile("http://192.168.2.133:9996/test/financing/servingfile.do",multiValueMap,Result.class);
     *         System.out.println(result3);
     */
}
